
package com.be.beadmin.quality.impl;

import com.be.beadmin.quality.dto.input.CabinetQualityDto;
import com.be.beadmin.quality.dto.input.CabinetQualityAndCabinetErrorDto;
import com.be.beadmin.quality.entity.CabinetError;
import com.be.beadmin.quality.entity.CabinetError_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.quality.mapper.CabinetErrorMapperEx;
import com.be.beadmin.quality.repository.CabinetErrorRepository;
import com.be.beadmin.quality.service.CabinetErrorService;
import com.be.beadmin.quality.dto.input.CabinetErrorDto;
import com.be.beadmin.quality.dto.output.CabinetErrorEx;
import com.be.beadmin.quality.dto.input.CabinetErrorQueryCriteria;
import com.be.beadmin.quality.mapper.CabinetErrorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author zty
* @date 2023-07-01
**/
@Service
@RequiredArgsConstructor
public class CabinetErrorServiceImpl implements CabinetErrorService {

    @Autowired
    private  CabinetErrorRepository cabinetErrorRepository;
    @Autowired
    private  CabinetErrorMapper cabinetErrorMapper;
    @Autowired
    private  CabinetErrorMapperEx cabinetErrorExMapper;

    @Override
    public Page<CabinetErrorEx> getCabinetErrorByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<CabinetError> page = cabinetErrorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(CabinetError_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(CabinetError_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<CabinetErrorEx> page1 = page.map(cabinetErrorExMapper::toEntity);
            return page1;

    }

    @Override
    public void addCabinetError(CabinetErrorDto cabinetErrorDto) {
            CabinetError result = cabinetErrorRepository.findCabinetErrorByIdAndDelete(cabinetErrorDto.getId(), 0);
            if (ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("CabinetError不存在,请重新查询");
            }
            CabinetError cabinetError = cabinetErrorMapper.toEntity(cabinetErrorDto);
            cabinetErrorRepository.save(cabinetError);

    }

    @Override
    public void updateCabinetError(CabinetErrorDto cabinetErrorDto) {
            CabinetError cabinetError = cabinetErrorRepository.findCabinetErrorById(cabinetErrorDto.getId());
            cabinetError = cabinetErrorMapper.toEntity(cabinetErrorDto);
            cabinetErrorRepository.save(cabinetError);
    }


    @Override
    public void delCabinetError(String id) {
        CabinetError cabinetError = cabinetErrorRepository.findCabinetErrorById(id);
        cabinetError.setDelete(1);
        cabinetErrorRepository.save(cabinetError);
    }

    @Override
    public void delCabinetErrors (List<String> ids) { }

    @Override
    public List<CabinetError> getCabinetError(String Id) {
        List<CabinetError> cabinetErrors = cabinetErrorRepository.findCabinetErrorByCabinetQualityIdAndDelete(Id, 0);
        return cabinetErrors;
    }

    @Override
    public int countCabinetError(CabinetQualityAndCabinetErrorDto cabinetQualityAndCabinetErrorDto) {
        CabinetErrorDto[] cabinetErrorDtos = cabinetQualityAndCabinetErrorDto.getCabinetErrorDtos();
        int count = 0;
        int sum = 0;
        for (int i = 0; i < cabinetErrorDtos.length; i++) {
            CabinetErrorDto cabinetErrorDto = cabinetErrorDtos[i];
            count = cabinetErrorDto.getErrorLineCount();
            sum = sum + count;
        }
        return sum;
    }
}